package com.shk.service.imp;


import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.shk.dao.PermissionDao;
import com.shk.dao.RoleDao;
import com.shk.dao.UserDao;
import com.shk.entity.PageResult;
import com.shk.pojo.Permission;
import com.shk.pojo.Role;
import com.shk.pojo.User;
import com.shk.service.UserService;
import com.shk.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

@Service(interfaceClass = UserService.class)
@Transactional
public class UserServiceImpl implements UserService{
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private PermissionDao permissionDao;

    @Override
    public User findByUsername(String username) {
        /*通过姓名拿到用户信息*/
       User user= userDao.findByUserName(username);
       if(user==null){
           return null;
       }
        Integer userId = user.getId();
        /*通过user  id 拿到角色信息*/
       Set<Role> roles= roleDao.findByUserId(userId);
       if(roles!=null && roles.size()>0){
           for(Role role:roles){
               Integer roleId = role.getId();
               Set<Permission> permissions = permissionDao.findByRoleId(roleId);
               role.setPermissions(permissions);
           }
           user.setRoles(roles);
       }

        return user;

    }


    @Override
    public void add(User user, Integer[] roleIds) {
        String password = MD5Utils.md5(user.getPassword());
        String deleteCode = user.getDeleteCode();
        String gender = user.getGender();
        System.out.println(gender);
        if (deleteCode == null) {
            user.setDeleteCode("0");
        }
        user.setPassword(password);
        userDao.add(user);
        setUserAndRole(user.getId(),roleIds);
    }

    @Override
    public PageResult findPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage,pageSize);
       Page<User> data=userDao.findPage(queryString);

        return new PageResult(data.getTotal(),data.getResult());
    }

    @Override
    public void delete(Integer id) {
        deleteUserAndRole(id);
        userDao.delete(id);
    }

    @Override
    public void update(User user, Integer[] roleIds) {
        deleteUserAndRole(user.getId());
        userDao.update(user);
        setUserAndRole(user.getId(),roleIds);
    }

    @Override
    public Map findRoleIdsByUserId(Integer userId) {
        Map map = new HashMap<>();
        List<Role> all = roleDao.findAll();
        User user = userDao.findById(userId);
        List<Integer> list= userDao.findRoleByUserId(userId);
        map.put("roles",all);
        map.put("user",user);
        map.put("roleIds",list);
        return map;
    }


    /*删除用户和角色关系*/
    public void deleteUserAndRole(Integer id){
        userDao.deleteUserAndRole(id);
    }
    /*建立用户和角色关系*/
    public void setUserAndRole(Integer userId,Integer[] roleIds){
        if(userId !=null){
            if(roleIds != null && roleIds.length>0){
                for(Integer roleId:roleIds){
                    Map map = new HashMap<>();
                    map.put("user_id",userId);
                            map.put("role_id",roleId);
                            userDao.setUserAndRole(map);
                }
            }
        }
    }

}
